$(function(){
	//获取最近一次监测时间
	$.get(contextPath+'/YiZhengHome/getLatestDate.dhc').done(function(result){
		var latestDate = result.data;
		if(latestDate!=null)
		{
			var dateArr =latestDate.split("-");
		    var endDate =dateArr[0]+"-"+dateArr[1]+"-"+dateArr[2];
		    var startDate = getLastMonthYestdy(new Date(dateArr[0]+"/"+dateArr[1]+"/"+dateArr[2]));
		    $('#'+tabId+' input#edate').val(endDate);
			$('#'+tabId+' input#sdate').val(startDate);
		    
		}
		medAffCaseAnalysisSearch();
	});
})
//获取上个月的前一天
 function getLastMonthYestdy(date){   
     var daysInMonth = new Array([0],[31],[28],[31],[30],[31],[30],[31],[31],[30],[31],[30],[31]);   
     var strYear = date.getFullYear();     
     var strDay = date.getDate();     
     var strMonth = date.getMonth()+1;   
     if(strYear%4 == 0 && strYear%100 != 0){   
        daysInMonth[2] = 29;   
     }   
     if(strMonth - 1 == 0)   
     {   
        strYear -= 1;   
        strMonth = 12;   
     }   
     else  
     {   
        strMonth -= 1;   
     }   
     strDay = daysInMonth[strMonth] >= strDay ? strDay : daysInMonth[strMonth];   
     if(strMonth<10)     
     {     
        strMonth="0"+strMonth;     
     }   
     if(strDay<10)     
     {     
        strDay="0"+strDay;     
     }   
     var datastr = strYear+"-"+strMonth+"-"+strDay;   
     return datastr;   
  }   

function medAffCaseAnalysisSearch(){
	
	var s = $('#'+tabId+' input#sdate').val();
	var e = $('#'+tabId+' input#edate').val();
	var data = "s="+s+"&e="+e;
	$.post(contextPath + "/YiZhengHome/getTotalAnalysis.dhc",data).done(function(result){
		var coi = result.data.common[0];//case (of illness)  病例
		$('#'+tabId+' #allTotal').html(coi[0]);
		if(coi[1]){
			$('#'+tabId+' #over60Total').html("<a href=\"javascript:getCasesFromOver60()\">"+coi[1]+"</a>");
		}else{
			$('#'+tabId+' #over60Total').html("");
		}
		$('#'+tabId+' #inGroupTotal').html(coi[3]);
		$('#'+tabId+' #notInGroupTotal').html(coi[2]);
		var dead = result.data.dead[0];//死亡相关病例
		if(dead[0]){
			$('#'+tabId+' #deadTotal').html("<a href=\"javascript:getCasesDead('0')\">"+dead[0]+"</a>");
		}else{
			$('#'+tabId+' #deadTotal').html("");
		}
		if(dead[1]){
			$('#'+tabId+' #dead1Total').html("<a href=\"javascript:getCasesDead('1')\">"+dead[1]+"</a>");
		}else{
			$('#'+tabId+' #dead1Total').html("");
		}
		if(dead[2]){
			$('#'+tabId+' #dead2Total').html("<a href=\"javascript:getCasesDead('2')\">"+dead[2]+"</a>");
		}else{
			$('#'+tabId+' #dead2Total').html("");
		}
	});
	

	var id='inGroupTable'+new Date().getTime();
	var str="";
	str+=' <table id="'+id+'" class="table table-striped" cellspacing="0" width="100%" role="grid" aria-describedby="example_info">  ';
	str+='  <caption class="contaotalsccq">入组病例统计</caption> ';
	str+=' 	<thead>  ';
	str+=' 		<tr>  ';
	str+=' 			<th tabindex="0" aria-controls="example" rowspan="1" colspan="1" aria-sort="ascending">病种</th>  ';
	str+=' 			<th tabindex="0" aria-controls="example" rowspan="1" colspan="1">病例数</th>  ';
	str+=' 			<th tabindex="0" aria-controls="example" rowspan="1" colspan="1">排名</th>  ';
	str+=' 		</tr>  ';
	str+=' 	</thead>  ';
	str+=' 	<tbody>  ';
	str+=' 	</tbody>  ';
	str+=' </table>  ';
	$('#'+tabId+' #inGroupTable .mytable').html(str);
	$.post(contextPath + "/YiZhengHome/getDisCaseInfo.dhc",data).done(function(result){
		var table=result.data;
		var columns=[ 
             { "data": "MDC_DESC1",
                	"render": function(obj, type, full){
                		return "<a href=\"javascript:getCaseDrg('"+full[0]+"','"+full[1]+"')\">"+full[1]+"</a>";
                	}
                 },
                 { "data": "CASECOUNT",
         	   "render": function(obj, type, full){
                		return  full[2];
         	   }	
                 },
                 { "data": "ROWINDEX",
                   "render": function(obj, type, full){
                    		return  full[3];
                	   }	
                 }
             ];
		var paramsDataTables={lengthChange:false,info:false,stateSave:true,scrollY: '380px',rownumbers:true,order:[[ 2, "desc" ]]};
		$('#'+tabId+' #'+id).dataTableBasic(columns,table,paramsDataTables);
	});
	$.post(contextPath + "/YiZhengHome/getOtherCase.dhc",data).done(function(result){
		var table=result.data;
		var columns=[ 
                     { "data": "NOTIN_CODE",
	                       "render": function(obj, type, full){
	                    	   var code = full[0];
	                    	   var name="";
	                    	   if(code=="0"){
	                    		   name="<a href=\"javascript:getCasesFromNotin('"+code+"')\">无法分组</a>";
	                    	   }else if(code=="QY"){
	                    		   name="<a href=\"javascript:getQYDetail('"+code+"','"+s+"','"+e+"')\">手术和诊断不符</a>";
	                    	   }else{
	                    		   name="其他原因";
	                    	   }
	                    	   return name;
	                       }},
	                       { 
	                    	   "data": "CASECOUNT",
	                    	   "render": function(obj, type, full){
		                    	   return full[1];
	                       }
                  	 }
	                 ];
		var paramsDataTables={lengthChange:false,info:false,stateSave:true,rownumbers:true};
		$('#'+tabId+' #notinGroupTableIncode').dataTableBasic(columns,table,paramsDataTables);
	});
	
	$.post(contextPath + "/YiZhengHome/getDepCaseData.dhc",data).done(function(result){

		var table=result.data;
		var columns=[ 
                 { "data": "DEPDESC",
              	   "render": function(obj, type, full){
                  	   return full[2];
                     }
              	 },
                   { "data": "CASECNT1",
                     "render": function(obj, type, full){
                  	   return "<a href=\"javascript:getCasesFromDep('"+full[1]+"','"+full[2]+"')\">" + full[0]+"</a>";
                     }
                   },
                   { "data": "TOTALCSECNT",
                     "render": function(obj, type, full){
                  	   return full[3];
                     }
                   },
                   { "data": "Ratio",
                     "render": function(obj, type, full){
                  	   return (full[0]*100/full[3]).toFixed(2)+"%"
                     }
                   }
               ];
		var paramsDataTables={lengthChange:false,searching:false,info:false,stateSave:true,scrollY: '280px',order:[[ 1, "desc" ]],rownumbers:true};
		$('#'+tabId+' #notinGroupTableDep').dataTableBasic(columns,table,paramsDataTables);
	
	});
	
}

function getQYDetail(mdcCode,s,e){
	var str="";
	str+='<tr><td width="100%"><h4>未入组原因-手术和诊断不符</h4></td></tr>';
	str+='<tr>';
	str+='<td width="100%" id="'+mdcCode+'TablePop" style="vertical-align:2px">';
	str+='<div style="text-align: center"><h5>详情分析</h5></div>';
	str+='<div class="mytable"></div>';
	str+='</td>';
	str+="</tr>";
	$('#'+tabId+' #caseAnalysisDRGId').html(str);
	var id=mdcCode+"Table"+new Date().getTime();
	$('#'+tabId+' #'+mdcCode+'TablePop .mytable').html('<table id="'+id+'" class="stripe cc" style="width:100%">'
			+'<thead><tr><td>原因</td>'
			+'<td>病例数</td>'
			+'<td>排名</td>'
			+'</tr></thead><tbody></tbody></table>');
	var data = "startDate="+s+"&endDate="+e+"&notincode="+mdcCode;
	$.post(contextPath + "/YiZhengHome/getCaseQYData.dhc",data).done(function(result){
		var table=result.data;
		var columns=[ 
                     { "data": "DRG_DESC",
	                       "render": function(obj, type, full){
	                    	   var code = full[0];
	                    	   var codex="";
	                    	   var name="";
	                    	   if(code.indexOf("QY")>0){
	                    		   codex=code.replace("QY","");
	                    	   }
	                    	   if(codex!=""){
	                    		   name="<a href=\"javascript:getCasesFromNotin('"+code+"')\">手术和诊断不符("+codex+")</a>";
	                    	   }else{
	                    		   name="手术和诊断不符(其他)";
	                    	   }
	                    	   return name;
	                       }},
	                     { "data": "CASECOUNT",
	                    	   "render": function(obj, type, full){
		                    	   return full[1];
		                       }
	                    	   
                  	 },
	                     { "data": "ROWINDEX",
                  		 "render": function(obj, type, full){
		                    	   return full[2];
		                       }
                  	 }
	                 ];
		var paramsDataTables={lengthChange:false,searching:false,info:false,stateSave:true,scrollY: '360px',order:[[ 2, "asc" ]],rownumbers:true};
		$('#'+tabId+' #'+id).dataTableBasic(columns,table,paramsDataTables);
	
	});
	$('#'+tabId+' #caseAnalysisDRG').addClass('popoverActive');
}

//查看drg
function getCaseDrg(mdcCode,mdcDesc){
	scrollTo(0,0);
	var str="";
	str+='<tr><td width="100%"><h4>'+mdcDesc+'('+mdcCode+')'+'</h4></td></tr>';
	str+='<tr>';
	str+='<td width="100%" id="'+mdcCode+'TablePop" style="vertical-align:2px">';
	str+='<div style="text-align: center"><h5>'+mdcDesc+'病例统计</h5></div>';
	str+='<div id="head_'+mdcCode+'Pop"></div>';
	str+='<div class="mytable"></div>';
	str+='</td>';
	str+="</tr>";
	$('#'+tabId+' #caseAnalysisDRGId').html(str);
	var id=mdcCode+"Table"+new Date().getTime();
	$('#'+tabId+' #'+mdcCode+'TablePop .mytable').html('<table id="'+id+'" class="stripe cc" style="width:100%">'
			+'<thead><tr><td>DRG</td>'
			+'<td>病例数</td>'
			+'<td>排名</td>'
			+'</tr></thead><tbody></tbody></table>');
	var s = $('#'+tabId+' input#sdate').val();
	var e = $('#'+tabId+' input#edate').val();
	var data = "s="+s+"&e="+e+"&mdcCode="+mdcCode;
	$.post(contextPath + "/YiZhengHome/getDrgDataInMdc.dhc",data).done(function(result){

		var table=result.data;
		var columns=[ 
                     { "data": "DRG_DESC",
	                    	"render": function(obj, type, full){
	                    		return "<a href=\"javascript:getCasesFromDRG('"+full[0]+"')\">"+full[1]+"</a>";
	                    	}
	                     },
	                     { "data": "CASECOUNT",
                 	   "render": function(obj, type, full){
	                    		return  full[2];
                 	   }	
	                     },
	                     { "data": "ROWINDEX",
	                       "render": function(obj, type, full){
		                    		return  full[3];
	                    	   }	
	                     }
              ];
		var paramsDataTables={lengthChange:false,searching:false,info:false,stateSave:true,scrollY: '360px',order:[[ 2, "asc" ]],rownumbers:true};
		$('#'+tabId+' #'+id).dataTableBasic(columns,table,paramsDataTables);
	
	});
	
	$('#'+tabId+' #caseAnalysisDRG').addClass('popoverActive');
}

function medAffCaseAnalysisClosePop(){
	$('#'+tabId+' #caseAnalysisDRG').removeClass('popoverActive');
}

function getCasesFromDRG(drgCode){
	var s = $('#'+tabId+' input#sdate').val();
	var e = $('#'+tabId+' input#edate').val();
	openTab('/pages/yizheng/caseDtl.jsp',drgCode+'|病例', drgCode+'caseInfo',drgCode+'caseInfo',{startDate:s,endDate:e,drgCode:drgCode});
}

function getCasesFromOver60(){
	var s = $('#'+tabId+' input#sdate').val();
	var e = $('#'+tabId+' input#edate').val();
	openTab('/pages/yizheng/caseDtl.jsp','大于60天病例', 'over60dayscase','over60dayscase',{startDate:s,endDate:e,over60:"1"});
} 

function getCasesFromNotin(notinCode){
	var s = $('#'+tabId+' input#sdate').val();
	var e = $('#'+tabId+' input#edate').val();
	openTab('/pages/yizheng/caseDtl.jsp','未入组病例', 'notincase'+notinCode,'notincase'+notinCode,{startDate:s,endDate:e,notInCode:notinCode});
}

function getCasesDead(riskLevel){
	var s = $('#'+tabId+' input#sdate').val();
	var e = $('#'+tabId+' input#edate').val();
	var deadLevel = "";
	if(riskLevel==0)
	{
		deadLevel="死亡病例";
	}
	else if(riskLevel==1)
	{
		deadLevel="低风险死亡病例";
	}
	else
	{
		deadLevel="中低风险死亡病例";
	}
	openTab('/pages/yizheng/caseDtl.jsp',deadLevel, 'deadCase'+riskLevel,'deadCase'+riskLevel,{startDate:s,endDate:e,riskLevel:riskLevel});
}

function getCasesFromDep(depCode,depName){
	var s = $('#'+tabId+' input#sdate').val();
	var e = $('#'+tabId+' input#edate').val();
	openTab('/pages/yizheng/caseDtl.jsp',depName+"|病例", 'depCase'+depCode,'depCase'+depCode,{startDate:s,endDate:e,depCode:depCode,notInCode:"all"});
}